package org.finesys.chat.core.base.segment;

import org.finesys.chat.core.base.document.Metadata;
import org.finesys.common.core.util.ValidationUtil;

import java.util.Objects;

public class TextSegment {

    private final String text;
    private final Metadata metadata;

    public TextSegment(String text, Metadata metadata) {
        this.text = ValidationUtil.ensureNotBlank(text, "text");
        this.metadata = ValidationUtil.ensureNotNull(metadata, "metadata");
    }

    public String getText() {
        return text;
    }

    public Metadata getMetadata() {
        return metadata;
    }

    public static TextSegment from(String text, Metadata metadata) {
        return new TextSegment(text, metadata);
    }

    public static TextSegment from(String text) {
        return from(text, new Metadata());
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;
        TextSegment that = (TextSegment) object;
        return Objects.equals(text, that.text) && Objects.equals(metadata, that.metadata);
    }

    @Override
    public int hashCode() {
        return Objects.hash(text, metadata);
    }

    @Override
    public String toString() {
        return "TextSegent{" +
                "text='" + text + '\'' +
                ", metadata=" + metadata +
                '}';
    }
}
